package com.example.demo.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.tools.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.demo.controller.Result;
import com.example.demo.sys.mapper.ClassdiscussMapper;
import com.example.demo.sys.entity.Classdiscuss;

import org.springframework.web.bind.annotation.RestController;
import com.example.demo.common.tools.BaseController;

/**
 * <p>
 *  前端控制器 chrimer
 * </p>
 *
 * @author chrimer
 * @since 2020-06-27
 */
@RestController
@RequestMapping("/sys/classdiscuss")
public class ClassdiscussController extends BaseController {
    @Autowired
    private ClassdiscussMapper classdiscussMapper;

    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
            Integer pageNum,
            Integer pageSize,
            String courseId,
            String etc) {
        pageNum = Integer.valueOf(etc).intValue();
        pageSize = 8;
        Result result = new Result();
        Page<Classdiscuss> page = new Page<Classdiscuss>(pageNum, pageSize);
        Classdiscuss parms = new Classdiscuss();
        QueryWrapper<Classdiscuss> warpper = new QueryWrapper<Classdiscuss>(parms);

        if(courseId !=null && !"".equals(courseId)) {
            warpper.eq("courseId", courseId);
            warpper.orderByDesc("praise");
        }
        result.setData(classdiscussMapper.selectPage(page, warpper));
        return result;
    }
    //点赞
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody Classdiscuss entity) {
        Result result = new Result();
        //System.out.println(entity);
        if (entity.getId() == null) {//发表评论
            entity.setCreatTime(DateTool.getCurrTime());
            classdiscussMapper.insert(entity);
        } else {//点赞
            entity.setPraise(entity.getPraise()+1);
            classdiscussMapper.updateById(entity);
        }
        return result;
    }

    //查看课程评论信息总数
    @RequestMapping(method= RequestMethod.POST,value="/selectdiscussNum")
    public Result selectdiscussNum(String id) {
        Result result = new Result();
        Classdiscuss courseinfor = new Classdiscuss();
        QueryWrapper<Classdiscuss> warpper = new QueryWrapper<Classdiscuss>(courseinfor);
        List<Classdiscuss> Num = null;
        Num = classdiscussMapper.SelectNum(id);
        int discussNum = Num.size();
       // System.out.println(id);
        Map<String, Object> map = new HashMap<>();
        map.put("discussNum", discussNum);
        result.setData(map);
        return result;
    }


    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
        classdiscussMapper.deleteBatchIds(deleteIds);
        return result;
    }

}

